{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# work in progress\n",
    "\n",
    "# for experimental / educational purposes only"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from pathlib import Path\n",
    "PROJECT_DIR = Path.cwd()\n",
    "if PROJECT_DIR.stem == 'notebooks':\n",
    "    PROJECT_DIR = PROJECT_DIR.parent\n",
    "    sys.path.insert(0, '..')\n",
    "\n",
    "INTERACTIVE_PLOTTING = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from https://www.datacamp.com/workspace/templates/playbook-python-multiple-time-series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load packages\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from statsmodels.graphics import tsaplots\n",
    "import statsmodels.api as sm\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Upload your data as CSV and load as a data frame\n",
    "df = pd.read_csv(\n",
    "    \"data.csv\",\n",
    "    parse_dates=[\"datestamp\"],  # Tell pandas which column(s) to parse as dates\n",
    "    index_col=\"datestamp\",  # Use a date column as your index\n",
    ")\n",
    "df.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot settings\n",
    "%config InlineBackend.figure_format='retina'\n",
    "plt.rcParams[\"figure.figsize\"] = (18, 10)\n",
    "plt.style.use('ggplot')\n",
    "\n",
    "# Plot all time series in the df DataFrame\n",
    "ax = df.plot(\n",
    "    colormap=\"Spectral\",  # Set a colormap to avoid overlapping colors\n",
    "    fontsize=10,  # Set fontsize\n",
    "    linewidth=0.8, # Set width of lines\n",
    ")\n",
    "\n",
    "# Set labels and legend\n",
    "ax.set_xlabel(\"Date\", fontsize=12)  # X axis text\n",
    "ax.set_ylabel(\"Unemployment Rate\", fontsize=12) # Set font size\n",
    "ax.set_title(\"Unemployment rate of U.S. workers by industry\", fontsize=15)\n",
    "ax.legend(\n",
    "    loc=\"center left\",  # Set location of legend within bounding box\n",
    "    bbox_to_anchor=(1.0, 0.5),  # Set location of bounding box\n",
    ")\n",
    "\n",
    "# Annotate your plots with vertical lines\n",
    "ax.axvline(\n",
    "    \"2001-07-01\",  # Position of vertical line\n",
    "    color=\"red\",  # Color of line\n",
    "    linestyle=\"--\",  # Style of line\n",
    "    linewidth=2, # Thickness of line\n",
    ")\n",
    "ax.axvline(\"2008-09-01\", color=\"red\", linestyle=\"--\", linewidth=2)\n",
    "\n",
    "# Show plot\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  Generate a boxplot\n",
    "ax = df.boxplot(fontsize=10, vert=False)  # Plots boxplot horizonally if false\n",
    "ax.set_xlabel(\"Unemployment Percentage\")\n",
    "ax.set_title(\"Distribution of Unemployment by industry\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Display the autocorrelation plot of your time series\n",
    "fig = tsaplots.plot_acf(\n",
    "    df[\"Agriculture\"], lags=24  # Change column to inspect\n",
    ")  # Set lag period\n",
    "\n",
    "# Show plot\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Display the partial autocorrelation plot of your time series\n",
    "fig = tsaplots.plot_pacf(\n",
    "    df[\"Agriculture\"], lags=24  # Change column to inspect\n",
    ")  # Set lag period\n",
    "\n",
    "# Show plot\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Extract time period of interest\n",
    "index_year = df.index.year  # Choose year, month, day etc.\n",
    "\n",
    "# Compute mean for each time period\n",
    "df_by_year = df.groupby(index_year).mean()  # Replace .mean() with aggregation function\n",
    "\n",
    "# Plot the mean for each time period\n",
    "ax = df_by_year.plot(fontsize=10, linewidth=1)\n",
    "\n",
    "# Set axis labels and legend\n",
    "ax.set_xlabel(\"Year\", fontsize=12)\n",
    "ax.set_ylabel(\"Mean unemployment rate\", fontsize=12)\n",
    "ax.axvline(\n",
    "    2008,  # Position of vertical line\n",
    "    color=\"red\",  # Color of line\n",
    "    linestyle=\"--\",  # Style of line\n",
    "    linewidth=2,\n",
    ")  # Thickness of line\n",
    "\n",
    "ax.legend(\n",
    "    loc=\"center left\", bbox_to_anchor=(1.0, 0.5)  # Placement of legend within bbox\n",
    ")  # Location of boundary box (bbox)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run time series decomposition on each time series of the DataFrame\n",
    "df_names = df.columns\n",
    "df_decomp = {ts: sm.tsa.seasonal_decompose(df[ts]) for ts in df.columns}\n",
    "\n",
    "# Capture the seasonal, trend and noise components for the decomposition of each time series\n",
    "seasonal_dict = {ts: df_decomp[ts].seasonal for ts in df_names}\n",
    "trend_dict = {ts: df_decomp[ts].trend for ts in df_names}\n",
    "noise_dict = {ts: df_decomp[ts].resid for ts in df_names}\n",
    "\n",
    "# Create a DataFrame from the dictionaries\n",
    "seasonality_df = pd.DataFrame.from_dict(seasonal_dict)\n",
    "trend_df = pd.DataFrame.from_dict(trend_dict)\n",
    "noise_df = pd.DataFrame.from_dict(noise_dict)\n",
    "\n",
    "# Remove the label for the index\n",
    "seasonality_df.index.name = None\n",
    "trend_df.index.name = None\n",
    "noise_df.index.name = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Look at individual seasonality, trend or noise\n",
    "noise_df[\"Agriculture\"].plot()\n",
    "# Change the dataframe and colum to explore\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a faceted plot of the seasonality_df DataFrame\n",
    "trend_df[[\"Agriculture\", \"Manufacturing\"]].plot(\n",
    "    subplots=True,  # Show multiple plots\n",
    "    layout=(2, 1),  # Choose layout for showing plots\n",
    "    sharey=False,  # Share the y axis\n",
    "    legend=True,  # Show legend\n",
    "    fontsize=10,  # Set fontsize\n",
    "    linewidth=2,  # Set widht of line\n",
    ")\n",
    "\n",
    "plt.suptitle(\"Seasonality in Agriculture and Manufacturing\", size=15)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get correlation matrix of your chosen dataframe\n",
    "seasonality_corr = seasonality_df.corr(\n",
    "    method=\"spearman\"\n",
    ")  # Choose method to calculate correlation\n",
    "\n",
    "# Customize the clustermap of the correlation matrix\n",
    "fig = sns.clustermap(\n",
    "    seasonality_corr,  # Choose correlation matrix to visualize\n",
    "    annot=True,  # Show annotations\n",
    "    annot_kws={\"size\": 10},  # Customize annotations\n",
    "    linewidths=0.4,\n",
    "    figsize=(15, 10),\n",
    ")\n",
    "\n",
    "plt.setp(\n",
    "    fig.ax_heatmap.xaxis.get_majorticklabels(),\n",
    "    rotation=90,  # Change rotation of x-labels\n",
    ")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "psychopy",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
